package com.shg.web.ztest;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.shg.bean.BookType;
import com.shg.service.BookTypeService;

public class BookTypeServiceTest {

	private static BookTypeService bookTypeService;
	
	private Map<String, String> map=new LinkedHashMap<String, String>();
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ApplicationContext context=new ClassPathXmlApplicationContext("basics-application.xml");
		bookTypeService = (BookTypeService)context.getBean("bookTypeServiceBean");
	}

	@Test
	public void testInsert(){
		List<String> types=new ArrayList<String>();
		types.add("297ec07635dd59d00135dd59d4520000");
		types.add("297ec07635de04e20135de04e6d20000");
		
		Set<BookType> bookTypes=null;
		try {
			bookTypes = bookTypeService.getTypeList(types);
		} catch (Exception e) {
			e.printStackTrace();
		}
		for(BookType bookType:bookTypes){
			System.out.println(bookType.getName());
			for(BookType type:bookType.getTypes()){
				System.out.println(type.getParent().getName()+"------>"+type.getName());
				for(BookType type2:type.getTypes()){
					System.out.println("------>"+type2.getParent().getName()+"------>"+type2.getName());
					for(BookType type3:type2.getTypes())
						System.out.println("------>"+type2.getParent().getName()+"------>"+type3.getParent().getName()+"------>"+type3.getName());
				}
			}
		}
	}
	
	@Test
	public void testgTopTypeList(){
		List<BookType> list;
		try {
			list = bookTypeService.getSelectTypeList(null);
			for(BookType type:list){
				System.out.println(type.getName());
				for(BookType bookType:type.getTypes()){
					System.out.println("========"+bookType.getName());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void testGetChilds(){
		List<BookType> list;
		try {
			list = bookTypeService.getSelectTypeList("297ec07635de04130135de0417b90000");
			navigation(list.get(0));
			for(Iterator<String> item = map.keySet().iterator();item.hasNext();){
				String key = item.next();
				String value = map.get(key);
				System.out.println(key+"  "+value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void navigation(BookType type){
		if(type.getParent()!=null){
			navigation(type.getParent());
			//注意这里的位置一定不能颠倒
			map.put(type.getParent().getId(), type.getParent().getName());
		}
	}
	
	@Test
	public void testShopType(){
		Set<BookType> bookTypes=null;
		try {
			bookTypes = bookTypeService.getShopBookType("ff80808135a033220135a033246c0000");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(BookType bookType:bookTypes){
			System.out.println(bookType.getName());
			for(BookType type:bookType.getTypes()){
				System.out.println(type.getParent().getName()+"------>"+type.getName());
				for(BookType type2:type.getTypes()){
					System.out.println("------>"+type2.getParent().getName()+"------>"+type2.getName());
					for(BookType type3:type2.getTypes())
						System.out.println("------>"+type2.getParent().getName()+"------>"+type3.getParent().getName()+"------>"+type3.getName());
				}
			}
		}
	}
	
	@Test 
	public void getAllType(){
		List<BookType> list;
		try {
			list = bookTypeService.getAllTypeList(null);
			for(BookType type:list){
				System.out.println(type.getName());
				for(BookType bookType:type.getTypes()){
					System.out.println("——————————————"+bookType.getName());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
